package com.goldgov.authorityInfo.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.goldgov.authorityInfo.dao.AuthorityInfoDao;
import com.goldgov.authorityInfo.service.AuthorityInfo;
import com.goldgov.authorityInfo.service.AuthorityInfoExt;
import com.goldgov.authorityInfo.service.AuthorityInfoExtQuery;
import com.goldgov.authorityInfo.service.AuthorityInfoInit;
import com.goldgov.authorityInfo.service.AuthorityInfoQuery;
import com.goldgov.authorityInfo.service.AuthorityInfoService;
import com.goldgov.authorityInfo.service.OrganizationBean;
import com.goldgov.utils.PropertyUtil;

@Service
public class AuthorityInfoServiceImpl implements AuthorityInfoService {
	
	@Autowired
	private AuthorityInfoDao dao;

	@Override
	public void settingAuthorityInfo(AuthorityInfoQuery query) {
		//清空原有设置
		dao.clearAuthorityInfo(query);
		//重新进行设置
		List<AuthorityInfo> auths=JSONArray.parseArray(query.getOrgScopeStr(), AuthorityInfo.class);
		if(null!=auths) {
			for(AuthorityInfo a:auths) {
				a.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				a.setCreateUserID(query.getCurrentUserID());
				dao.settingAuthorityInfo(a);
			}
		}
	}

	@Override
	public void settingAdminAndScope(AuthorityInfoQuery query) {
		if(dao.existAdmin(query)==0) {
			//设置管理员
			dao.settingAdmin(query);
			//设置管理范围
			dao.settingOrgAdminScope(query);
		}
	}
	
	@Override
	public List<AuthorityInfoExt> findAuthorityInfo(AuthorityInfoExtQuery query) {
		//判断是否是绑定管理员
		boolean isBindingAdmin=false;
		//判断是否是绑定管理员，如果是则查询绑定管理员的数据权限
		AuthorityInfo a=dao.ifBindingAdmin(query);
		if(!PropertyUtil.objectNotEmpty(a)) {
			//管理员不存在时返回空
			return new ArrayList<>();
		}else if(PropertyUtil.objectNotEmpty(a)&&PropertyUtil.objectNotEmpty(a.getBindingAdminID())){
			query.setSearchUserID(a.getBindingAdminID());
			isBindingAdmin=true;
		}
		//查询权限列表
		List<AuthorityInfo> list=dao.findAuthorityList(query);
		List<AuthorityInfoExt> resultList=new ArrayList<>();
		//当未设置权限范围时查询默认范围
		if(list.isEmpty()) {
			//根据机构树ID查询中文名称
			AuthorityInfo orgInfo=dao.findOrganizationInfoByAdminID(query);
			if(!PropertyUtil.objectNotEmpty(orgInfo)) {
				//绑定管理员不存在时返回空
				return new ArrayList<>();
			}
			AuthorityInfo ai;
			for(String key:AuthorityInfoInit.getFunctionMap().keySet()) {
				ai=new AuthorityInfo();
				ai.setFunctionCode(key);
				ai.setFunctionName(AuthorityInfoInit.getFunctionMap().get(key));
				ai.setScopeCode(orgInfo.getScopeCode());
				ai.setOrganizationID(orgInfo.getOrganizationID());
				ai.setOrganizationCode(orgInfo.getOrganizationCode());
				ai.setOrganizationName(orgInfo.getOrganizationName());
				ai.setUserID(query.getSearchUserID());
				list.add(ai);
			}
		}
		Map<String,List<AuthorityInfo>> map=new LinkedHashMap<>();
		List<AuthorityInfo> aiList;
		//列表转换
		for(AuthorityInfo ai:list) {
			if(null==map.get(ai.getScopeCode())) {
				aiList=new ArrayList<>();
				aiList.add(ai);
				map.put(ai.getScopeCode(), aiList);
			}else {
				aiList=map.get(ai.getScopeCode());
				aiList.add(ai);
				map.put(ai.getScopeCode(), aiList);
			}
		}
		AuthorityInfoExt aie;
		Iterator<String> ite=map.keySet().iterator();
		while(ite.hasNext()) {
			aie=new AuthorityInfoExt();
			OrganizationBean o=aie.getOrganizationBean();
			o.setScopeCode(ite.next());
			aie.setList(map.get(o.getScopeCode()));
			o.setOrganizationId(aie.getList().get(0).getOrganizationID());
			o.setOrganizationName(aie.getList().get(0).getOrganizationName());
			o.setId(o.getOrganizationId());
			o.setTitle(o.getOrganizationName());
			aie.setOrganizationBean(o);
			aie.setIfBindingAdmin(isBindingAdmin);
			resultList.add(aie);
		}
		return resultList;
	}

	@Override
	public void clearAuthorityInfo(AuthorityInfoQuery query) {
		dao.clearAuthorityInfo(query);
	}

	@Override
	public void cancelAdmin(AuthorityInfoQuery query) {
		dao.cancelAdmin(query);
		dao.cancelOrgAdminScope(query);
		dao.cancelRole(query);
	}

	@Override
	public void bindingAdmin(AuthorityInfoQuery query) {
		//绑定之前先清空设置
		cancelAdmin(query);
		//然后再设置成管理员
		settingAdminAndScope(query);
		//绑定管理员
		dao.bindingAdmin(query);
		//绑定完成后设定角色
		dao.settingRole(query);
	}
	@Override
	public List<AuthorityInfo> findAdminList(AuthorityInfoQuery query){
		return dao.findAdminList(query);
	}
}